﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Net;
using System.Web.Mvc;

namespace MCSkinAssembler.Controllers
{
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			return View();
		}

		public ActionResult GetPlayerPhoto(string name, int scale)
		{
			int height = (scale * 32) + (scale / 2);
			int width = scale * 55;
			string fileName = string.Format("/Content/Images/PlayerSkins/{0}{1}.png", name, scale);
			Bitmap orginalImage = null;
			try
			{
				using (HttpWebResponse httpWebResponse = (HttpWebResponse)WebRequest.Create(string.Format("http://skins.minecraft.net/MinecraftSkins/{0}.png", name)).GetResponse())
				{
					orginalImage = new Bitmap(httpWebResponse.GetResponseStream());
				}
			}
			catch
			{
				using (BinaryReader reader = new BinaryReader(System.IO.File.Open(this.Server.MapPath("~/Content/Images/default_skin.png"), FileMode.Open)))
				{
					orginalImage = new Bitmap(reader.BaseStream);
				}
			}
			using (Bitmap assembled_skin = new Bitmap(width, height))
			{
				using (Graphics graphics = Graphics.FromImage(assembled_skin))
				{
					graphics.SmoothingMode = SmoothingMode.None;
					graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
					graphics.PixelOffsetMode = PixelOffsetMode.Half;

					#region Front

					//Head
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 4f, scale * .5f, scale * 8f, scale * 8f), GetRectangleF(8f, 8f, 8f, 8f), GraphicsUnit.Pixel);
					//Right Arm
					graphics.DrawImage(orginalImage, GetRectangleF(0f, scale * 8.5f, scale * 4f, scale * 12f), GetRectangleF(44f, 20f, 4f, 12f), GraphicsUnit.Pixel);
					//Torso
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 4f, scale * 8.5f, scale * 8f, scale * 12f), GetRectangleF(20f, 20f, 8f, 12f), GraphicsUnit.Pixel);
					//Left Arm
					orginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 12f, scale * 8.5f, scale * 4f, scale * 12f), GetRectangleF(16f, 20f, 4f, 12f), GraphicsUnit.Pixel);
					orginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
					//Right Leg
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 4f, scale * 20.5f, scale * 4f, scale * 12f), GetRectangleF(4f, 20f, 4f, 12f), GraphicsUnit.Pixel);
					//Left Leg
					orginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 8f, scale * 20.5f, scale * 4f, scale * 12f), GetRectangleF(56f, 20f, 4f, 12f), GraphicsUnit.Pixel);
					orginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
					//Mask
					graphics.DrawImage(orginalImage, GetRectangleF((scale * 4f) - (scale * .5f), 0f, scale * 9f, scale * 9f), GetRectangleF(40f, 8f, 8f, 8f), GraphicsUnit.Pixel);

					#endregion

					#region Left

					//Head
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 18f, scale * .5f, scale * 8f, scale * 8f), GetRectangleF(16f, 8f, 8f, 8f), GraphicsUnit.Pixel);
					//Left Arm
					orginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 20, scale * 8.5f, scale * 4f, scale * 12f), GetRectangleF(20f, 20f, 4f, 12f), GraphicsUnit.Pixel);
					orginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
					//Left Leg
					orginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 20, scale * 20.5f, scale * 4f, scale * 12f), GetRectangleF(60f, 20f, 4f, 12f), GraphicsUnit.Pixel);
					orginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
					//Mask
					graphics.DrawImage(orginalImage, GetRectangleF((scale * 18f) - (scale * .5f), 0f, scale * 9f, scale * 9f), GetRectangleF(48f, 8f, 8f, 8f), GraphicsUnit.Pixel);

					#endregion

					#region Back

					//Head
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 32f, scale * .5f, scale * 8f, scale * 8f), GetRectangleF(24f, 8f, 8f, 8f), GraphicsUnit.Pixel);
					//Right Arm
					orginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 28f, scale * 8.5f, scale * 4f, scale * 12f), GetRectangleF(8f, 20f, 4f, 12f), GraphicsUnit.Pixel);
					orginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
					//Torso
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 32f, scale * 8.5f, scale * 8f, scale * 12f), GetRectangleF(32f, 20f, 8f, 12f), GraphicsUnit.Pixel);
					//Left Arm
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 40f, scale * 8.5f, scale * 4f, scale * 12f), GetRectangleF(52f, 20f, 4f, 12f), GraphicsUnit.Pixel);
					//Right Leg
					orginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 32f, scale * 20.5f, scale * 4f, scale * 12f), GetRectangleF(48f, 20f, 4f, 12f), GraphicsUnit.Pixel);
					orginalImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
					//Left Leg
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 36f, scale * 20.5f, scale * 4f, scale * 12f), GetRectangleF(12f, 20f, 4f, 12f), GraphicsUnit.Pixel);
					//Mask
					graphics.DrawImage(orginalImage, GetRectangleF((scale * 32f) - (scale * .5f), 0f, scale * 9f, scale * 9f), GetRectangleF(56f, 8f, 8f, 8f), GraphicsUnit.Pixel);

					#endregion

					#region Right

					//Head
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 46f, scale * .5f, scale * 8f, scale * 8f), GetRectangleF(0f, 8f, 8f, 8f), GraphicsUnit.Pixel);
					//Right Arm
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 48f, scale * 8.5f, scale * 4f, scale * 12f), GetRectangleF(40f, 20f, 4f, 12f), GraphicsUnit.Pixel);
					//Right Leg
					graphics.DrawImage(orginalImage, GetRectangleF(scale * 48f, scale * 20.5f, scale * 4f, scale * 12f), GetRectangleF(0f, 20f, 4f, 12f), GraphicsUnit.Pixel);
					//Mask
					graphics.DrawImage(orginalImage, GetRectangleF((scale * 46f) - (scale * .5f), 0f, scale * 9f, scale * 9f), GetRectangleF(32f, 8f, 8f, 8f), GraphicsUnit.Pixel);

					#endregion

				}
				assembled_skin.Save(this.Server.MapPath(fileName));
				orginalImage.Dispose();
			}

			return PartialView("_PlayerPhoto", fileName);
		}

		private RectangleF GetRectangleF(float x, float y, float width, float height)
		{
			return new RectangleF(new PointF(x, y), new SizeF(width, height));
		}
	}
}
